package y24.m5.file;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class CountScore {
    File student;
    File score;

    public CountScore(File student, File score) {
        this.student = student;
        this.score = score;
    }

    public boolean count(){
        try (FileReader file = new FileReader(student)) {
            Scanner scanner = new Scanner(file);
            double sum = 0;
            double max = Double.MIN_VALUE;
            double min = Double.MAX_VALUE;
            int count = 0;
            while (scanner.hasNextLine()) {
                String[] scores = scanner.nextLine().split(" ");
                double score = Double.parseDouble(scores[2]);
                sum += score;
                max = Math.max(max, score);
                min = Math.min(min, score);
                count++;
            }

            double average = sum / count;

            try (FileWriter fileWriter = new FileWriter(score)) {
                fileWriter.write("最大值： " + max + "\n");
                fileWriter.write("最小值： " + min + "\n");
                fileWriter.write("平均值： " + average + "\n");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}
